//
//  DYCommonSCell.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYCommonSCell.h"
@interface DYCommonSCell ()
@property (nonatomic, strong) UIButton *seletedBtn;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) YXContactsModel *model;
@end

@implementation DYCommonSCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor colorWithHexString:@"#EDF4FF"];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self configSubView];
    }
    return self;
}

-(void)setFrame:(CGRect)frame
{
    frame.origin.x = 10;//这里间距为10，可以根据自己的情况调整
    frame.size.width -= 2 * frame.origin.x;
    frame.size.height -= 2 * frame.origin.x;
    [super setFrame:frame];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 12.0;
    UIImage *bk = [UIImage imageNamed:@"bk"];
    [self.seletedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
        make.centerY.mas_offset(0);
        make.width.height.mas_equalTo(bk.size.width);
    }];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.seletedBtn.mas_left).mas_offset(-5);
        make.top.bottom.left.mas_offset(0);
    }];
    
    
    if (self.detailLabel.hidden) {
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.containerView.mas_left).mas_offset(10);
            make.centerY.mas_equalTo(self.containerView.mas_centerY);
        }];
    }else{
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.containerView.mas_left).mas_offset(10);
            make.top.mas_offset(10);
        }];
        [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.containerView.mas_left).mas_offset(10);
            make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_offset(5);
        }];
    }

}

- (void)configSubView{
    [self.contentView addSubview:self.seletedBtn];
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.nameLabel];
    [self.containerView addSubview:self.detailLabel];
   
}

- (void)btnDidClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    !self.seletedBtnBlock ?:self.seletedBtnBlock(self.model, sender.selected);
}

- (void)bindWithModel:(YXContactsModel *)item seletedArray:(NSMutableArray *)seletedArray{
    self.model = item;
    BOOL isSeleted = [seletedArray containsObject:item];
    if (isSeleted){
        self.seletedBtn.selected = YES;
    }else{
        self.seletedBtn.selected = NO;
    }
}

- (void)setModel:(YXContactsModel *)model{
    _model = model;
    if (!model.name.length&&model.mobile.length) {
        self.nameLabel.text = model.mobile;
        self.detailLabel.hidden = YES;
    }else if(model.name.length&&!model.mobile.length){
        self.nameLabel.text = model.name;
        self.detailLabel.hidden = YES;
    }else{
        self.detailLabel.hidden = NO;
        self.nameLabel.text = model.name;
        self.detailLabel.text = model.mobile;
    }
    
    [self layoutIfNeeded];
    [self setNeedsLayout];
}

#pragma mark --setter/getter

- (UIView *)containerView{
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
    }
    return _containerView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:16 weight:3];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#323232"];
    }
    return _nameLabel;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:14];
        _detailLabel.textColor = [UIColor colorWithHexString:@"#323232"];
    }
    return _detailLabel;
}

- (UIButton *)seletedBtn{
    if (!_seletedBtn) {
        _seletedBtn = [[UIButton alloc] init];
        [_seletedBtn setImage:[UIImage imageNamed:@"xz"] forState:UIControlStateSelected];
        [_seletedBtn setImage:[UIImage imageNamed:@"bk"] forState:UIControlStateNormal];
        [_seletedBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seletedBtn;
}

@end
